package com.jingan.nursinghome.regulatory.dialog.businessside;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.R;
import com.jingan.nursinghome.regulatory.model.Topicinfo;
import com.jingan.nursinghome.regulatory.view.RectificationView;
import com.jingan.nursinghome.regulatory.view.SpannerView;

import java.util.ArrayList;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.base.BaseActivity;
import cn.android.support.v7.lib.sin.crown.kotlin.dialog.ViewPagerDialog;
import cn.android.support.v7.lib.sin.crown.kotlin.https.Bitmaps;
import cn.android.support.v7.lib.sin.crown.kotlin.widget.RoundTextView;
import cn.android.support.v7.lib.sin.crown.utils.AssetsUtils;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;
import cn.android.support.v7.lib.sin.crown.view.SingleChoose;

/**
 * Created by 彭治铭 on 2017/10/12.
 */

public class MicrometerTableAdapter2 extends RecyclerView.Adapter<MicrometerTableAdapter2.MyViewHolder> {
    BaseActivity baseActivity;

    Bitmap general;
    Bitmap selected;

    List<Topicinfo> topicinfoList;

    ViewPagerDialog viewPagerDialog;

    public ViewPagerDialog getViewPager() {
        if (viewPagerDialog == null) {
            viewPagerDialog = new ViewPagerDialog(baseActivity,true,true);
        }
        return viewPagerDialog;
//        return new ViewPagerDialog(baseActivity);
    }

    public MicrometerTableAdapter2(BaseActivity baseActivity, List<Topicinfo> topicinfoList) {
        this.baseActivity = baseActivity;
        this.topicinfoList = topicinfoList;
        general = AssetsUtils.getInstance().getBitmapFromAssets("crown/selector/general.png", 0, true);
        selected = AssetsUtils.getInstance().getBitmapFromAssets("crown/selector/selected.png", 0, true);
        general = ProportionUtils.getInstance().adapterBitmap(general);
        selected = ProportionUtils.getInstance().adapterBitmap(selected);
    }

    //ViewHolder和View绑定在一起，构造函数一般都是初始化控件
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView txt_title;
        TextView txt_question;//题目
        TextView tihao;//题号
        //        triangleView triangleView;
        SpannerView spanerScore;
        RectificationView rectifi;
        View ling;

        SingleChoose sp_type_no;//不违法
        SingleChoose sp_type;//违法

        EditText edit_remark;//备注
        RoundTextView view1, view2, view3;//图片
        List<RoundTextView> views = new ArrayList<>();

        public MyViewHolder(View itemView) {
            super(itemView);
            view1 = itemView.findViewById(R.id.view1);
            view2 = itemView.findViewById(R.id.view2);
            view3 = itemView.findViewById(R.id.view3);
            views.clear();
            views.add(view1);
            views.add(view2);
            views.add(view3);
            edit_remark = itemView.findViewById(R.id.edit_remark);
            txt_title = (TextView) itemView.findViewById(R.id.txt_title);
            txt_question = (TextView) itemView.findViewById(R.id.txt_question);
            tihao = itemView.findViewById(R.id.tihao);
//            triangleView = (com.jingan.nursinghome.regulatory.view.triangleView) itemView.findViewById(R.id.triangleView);
            //ProportionUtils.getInstance().adapterTextView(txt_title);
            spanerScore = (SpannerView) itemView.findViewById(R.id.spanerScore);
            //ProportionUtils.getInstance().adapterView(spanerScore);
            rectifi = (RectificationView) itemView.findViewById(R.id.rectifi);
            ling = itemView.findViewById(R.id.ling);
            //ProportionUtils.getInstance().adapterView(ling);

            sp_type_no = itemView.findViewById(R.id.sp_type_no);
            sp_type = itemView.findViewById(R.id.sp_type);

            sp_type_no.setTextColor(Color.parseColor("#333333"));
            sp_type_no.setMession("不违法");
            sp_type_no.setCancel(false);
            sp_type_no.setBitmap(general, selected);

            sp_type.setTextColor(Color.parseColor("#333333"));
            sp_type.setMession("违法");
            sp_type.setCancel(false);
            sp_type.setBitmap(general, selected);
        }
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_micrometertablleos_item2_news, parent, false);//这个是正确的加载方式。参数3必须为false，否则报错。
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
//        ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(holder.txt_question.getLayoutParams());
//        if (layoutParams == null || holder.itemView == null) {
//            return;
//        }
//        layoutParams.leftToLeft = holder.itemView.findViewById(R.id.left).getId();
//        layoutParams.topToTop = holder.itemView.findViewById(R.id.top3).getId();
//        holder.txt_title.setText(null);
//        holder.triangleView.setInfo(null);
        holder.ling.setVisibility(View.VISIBLE);

        //Log.e("test","下标:\t"+position+"\t题号:\t"+topicinfoList.get(position).getTid());
//        holder.txt_question.setLayoutParams(layoutParams);
        //题目分类
        if (topicinfoList.get(position).getSubjectcategory_name() != null) {
            holder.txt_title.setVisibility(View.VISIBLE);
            holder.txt_title.setText(topicinfoList.get(position).getSubjectcategory_name());
        } else {
            holder.txt_title.setVisibility(View.GONE);
        }
        String i = (position + 1) + "";//题号
        if (i.length() == 1) {
            i = "0" + i;
        }
        if (topicinfoList.get(position).getYid() != null) {
            holder.tihao.setText(topicinfoList.get(position).getYid() + "、");//题号
        } else {
            holder.tihao.setText(i + "、");//题号
        }
        holder.txt_question.setText(topicinfoList.get(position).getTtitle());//题目
        final Topicinfo topicinfo = topicinfoList.get(position);
        String topic_type = topicinfo.getTopic_type();
        //Log.e("test", "题号:\t" + position + "\t类型:\t" + topic_type);
        //图片
        for (int i2 = 0; i2 < holder.views.size(); i2++) {
            holder.views.get(i2).setBackground(null);
        }
        for (int i2 = 0; i2 < topicinfoList.get(position).getBitmaps().size(); i2++) {
            holder.views.get(i2).setBackground(new BitmapDrawable(topicinfoList.get(position).getBitmaps().get(i2)));
        }
        if (topicinfoList.get(position).getBitmaps().size() < holder.views.size()) {
            holder.views.get(topicinfoList.get(position).getBitmaps().size()).setBackgroundResource(R.mipmap.p_itemt_img_2876);
        }

        //==========================================================================
        //加载网络图片
        final String url = topicinfoList.get(position).getTopic_imgyiurl();
        final String url2 = topicinfoList.get(position).getTopic_imgerurl();
        final String url3 = topicinfoList.get(position).getTopic_imgsanurl();
        if (url != null && topicinfoList.get(position).getBitmaps().size() <= 0) {
            holder.views.get(0).setVisibility(View.VISIBLE);
            //BitmapBuilder.Builder(url).execute(baseActivity, holder.views.get(0), null);
            new Bitmaps(url, baseActivity).view(holder.views.get(0)).Get(null);
            holder.views.get(0).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos=position;
                    String url = topicinfoList.get(pos).getTopic_imgyiurl();
                    String url2 = topicinfoList.get(pos).getTopic_imgerurl();
                    String url3 = topicinfoList.get(pos).getTopic_imgsanurl();
                    getViewPager().uris(url, url2, url3).currentItem(0).show();
                }
            });
        }


        if (url2 != null && topicinfoList.get(position).getBitmaps().size() <= 1) {
            holder.views.get(1).setVisibility(View.VISIBLE);
            //BitmapBuilder.Builder(url2).execute(baseActivity, holder.views.get(1), null);
            new Bitmaps(url2, baseActivity).view(holder.views.get(1)).Get(null);
            holder.views.get(1).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos=position;
                    String url = topicinfoList.get(pos).getTopic_imgyiurl();
                    String url2 = topicinfoList.get(pos).getTopic_imgerurl();
                    String url3 = topicinfoList.get(pos).getTopic_imgsanurl();
                    getViewPager().uris(url, url2, url3).currentItem(1).show();
                }
            });
        }


        if (url3 != null && topicinfoList.get(position).getBitmaps().size() <= 2) {
            holder.views.get(2).setVisibility(View.VISIBLE);
            //BitmapBuilder.Builder(url3).execute(baseActivity, holder.views.get(2), null);
            new Bitmaps(url3, baseActivity).view(holder.views.get(2)).Get(null);
            holder.views.get(2).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos=position;
                    String url = topicinfoList.get(pos).getTopic_imgyiurl();
                    String url2 = topicinfoList.get(pos).getTopic_imgerurl();
                    String url3 = topicinfoList.get(pos).getTopic_imgsanurl();
                    getViewPager().uris(url, url2, url3).currentItem(2).show();
                }
            });
        }

        //==========================================================================

        //相机拍照
//        View.OnClickListener onClickListener = new View.OnClickListener() {
//            @Override
//            public void onClick(final View view) {
//                ((MainActivity) baseActivity).openPhoto(topicinfoList.get(position).getSelectList(), new BaseListenner<List<LocalMedia>>() {
//                    @Override
//                    public void onResult(List<LocalMedia> selectList) {
//                        topicinfoList.get(position).setSelectList(selectList);
//                        topicinfoList.get(position).getBitmaps().clear();
//                        for (int i = 0; i < holder.views.size(); i++) {
//                            holder.views.get(i).setBackground(null);
//                        }
//                        for (int i = 0; i < selectList.size(); i++) {
//                            String path = selectList.get(i).getPath();//路径
//                            int inSampleSizde = AssetsUtils.getInstance().getBitmapSize(path) / holder.view1.getWidth();
//                            //Log.e("test","位图宽度：\t"+AssetsUtils.getInstance().getBitmapSize(path)+"\t控件宽度:\t"+holder.view1.getWidth()+"\tinSampleSizde:\t"+inSampleSizde);
//                            Bitmap bitmap = AssetsUtils.getInstance().getBitmapFromFile(path, true, inSampleSizde);//位图
//                            bitmap = ProportionUtils.getInstance().GeometricCompressionBitmap(bitmap, baseActivity.getResources().getDimension(R.dimen.x100), 1f);
//                            topicinfoList.get(position).getBitmaps().add(bitmap);
//                            holder.views.get(i).setBackground(new BitmapDrawable(bitmap));
//                        }
//                        if (selectList.size() < holder.views.size()) {
//                            holder.views.get(selectList.size()).setBackgroundResource(R.mipmap.p_itemt_img_2876);
//                        }
//
//                        //==========================================================================
//                        //加载网络图片
//                        String url = topicinfoList.get(position).getTopic_imgyiurl();
//                        if (url != null && topicinfoList.get(position).getBitmaps().size() <= 0) {
//                            holder.views.get(0).setVisibility(View.VISIBLE);
//                            BitmapBuilder.Builder(url).execute(baseActivity, holder.views.get(0), new BaseListenner<Bitmap>() {
//                                @Override
//                                public void onResult(Bitmap result) {
//
//                                }
//                            });
//                        }
//
//                        String url2 = topicinfoList.get(position).getTopic_imgerurl();
//                        if (url2 != null && topicinfoList.get(position).getBitmaps().size() <= 1) {
//                            holder.views.get(1).setVisibility(View.VISIBLE);
//                            BitmapBuilder.Builder(url2).execute(baseActivity, holder.views.get(1), new BaseListenner<Bitmap>() {
//                                @Override
//                                public void onResult(Bitmap result) {
//
//                                }
//                            });
//                        }
//
//
//                        String url3 = topicinfoList.get(position).getTopic_imgsanurl();
//                        if (url3 != null && topicinfoList.get(position).getBitmaps().size() <= 2) {
//                            holder.views.get(2).setVisibility(View.VISIBLE);
//                            BitmapBuilder.Builder(url3).execute(baseActivity, holder.views.get(2), new BaseListenner<Bitmap>() {
//                                @Override
//                                public void onResult(Bitmap result) {
//
//                                }
//                            });
//                        }
//
//                        //==========================================================================
//
//                    }
//                });
//            }
//        };
//        holder.view1.setOnClickListener(onClickListener);
//        holder.view2.setOnClickListener(onClickListener);
//        holder.view3.setOnClickListener(onClickListener);


        //备注
        final Boolean[] iskeyDown = {false};
        holder.edit_remark.setText(topicinfoList.get(position).getRemark());
        holder.edit_remark.setSelection(topicinfoList.get(position).getRemark().length());//光标位置
        holder.edit_remark.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                    iskeyDown[0] = true;
                } else {
                    iskeyDown[0] = false;
                }
                return false;
            }
        });
        holder.edit_remark.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (!b) {
                    if (position < topicinfoList.size()) {
                        topicinfoList.get(position).setRemark(holder.edit_remark.getText().toString());
                    }
                }
            }
        });
        holder.edit_remark.setEnabled(false);
        if (topic_type == null) {
            topic_type = "1";
        }
        if (topic_type.trim().equals("1")) {
            //==========================================================================================小题类型
            holder.sp_type.setVisibility(View.INVISIBLE);
            holder.sp_type_no.setVisibility(View.INVISIBLE);
            holder.spanerScore.setVisibility(View.VISIBLE);
            holder.rectifi.setVisibility(View.VISIBLE);

            holder.itemView.findViewById(R.id.layut_weifa).setVisibility(View.GONE);//隐藏违法
            holder.itemView.findViewById(R.id.layut_dafen).setVisibility(View.VISIBLE);//显示打分
            //======================================================================================打分
            //holder.spanerScore.setInfo("  "+MicrometerTable.getInstance().getTopics().get(position).getMaxScore() + "分");//分数
            String cScore = topicinfoList.get(position).getPscore();
            if (cScore == null || cScore.trim().equals("")) {
                cScore = String.valueOf(topicinfoList.get(position).getTopic_score());//最大分
                holder.spanerScore.setIsscore(false);
            } else {
                holder.spanerScore.setIsscore(true);
            }
            holder.spanerScore.setInfoAndColor("  " + cScore + "分", Color.parseColor("#444444"));
            holder.spanerScore.setTextSize(ProportionUtils.getInstance().adapterInt(32));
//            holder.spanerScore.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (topicinfoList == null || topicinfoList.get(position) == null ||
//                            topicinfoList.get(position).getTopic_score() == null) {
//                        return;
//                    }
//                    String cS = topicinfoList.get(position).getPscore();
//                    if (cS == null || cS.trim().equals("")) {
//                        cS = String.valueOf(topicinfoList.get(position).getTopic_score());//最大分
//                    }
//                    //Log.e("test","cs:\t"+cS);
//                    ScoreDialog.getInstance(baseActivity, Integer.valueOf(cS), topicinfoList.get(position).getTopic_score(), new BaseListenner<Integer>() {
//                        @Override
//                        public void onResult(Integer result) {
//                            holder.spanerScore.setIsscore(true);
//                            holder.spanerScore.setInfoAndColor("  " + result + "分", Color.parseColor("#444444"));
//                            topicinfoList.get(position).setPscore(result + "");
//
//                            String c = topicinfoList.get(position).getPscore();
//                            String m = String.valueOf(topicinfoList.get(position).getTopic_score());
//                            if ((c == null || c.trim().equals("")) || !c.trim().equals(m.trim())) {
//                                //非满分状态。弹出是否整改的询问窗口。
//                                AlertDialog.getInstance(baseActivity, false, true).setMession("是否要整改？").setPositive("确认", new View.OnClickListener() {
//                                    @Override
//                                    public void onClick(View v) {
//                                        AlertDialog.dismisss();
//                                        ZhengaiDialog.getInstance(baseActivity, Integer.valueOf(topicinfoList.get(position).getZhengai()), new BaseListenner<Integer>() {
//                                            @Override
//                                            public void onResult(Integer result) {
//                                                topicinfoList.get(position).setZhengai(result + "");
//                                                if (topicinfoList.get(position).getZhengai().equals("0")) {
//                                                    holder.rectifi.setInfoAndColor("整改", Color.parseColor("#666666"));
//                                                }
//                                                if (topicinfoList.get(position).getZhengai().equals("1")) {
//                                                    holder.rectifi.setInfoAndColor("现场整改", Color.parseColor("#4B97F0"));
//                                                }
//                                                if (topicinfoList.get(position).getZhengai().equals("2")) {
//                                                    holder.rectifi.setInfoAndColor("计划整改", Color.parseColor("#4B97F0"));
//                                                }
//                                            }
//                                        });
//                                    }
//                                });
//                            }
//
//                        }
//                    });
//                }
//            });

            //==========================================================================================整改
//            Log.e("test","整改:\t"+topicinfoList.get(position).getZhengai());
            if (topicinfoList.get(position).getZhengai().equals("0")) {
                holder.rectifi.setInfoAndColor("整改", Color.parseColor("#666666"));
            }
            if (topicinfoList.get(position).getZhengai().equals("1")) {
                holder.rectifi.setInfoAndColor("现场整改", Color.parseColor("#4B97F0"));
            }
            if (topicinfoList.get(position).getZhengai().equals("2")) {
                holder.rectifi.setInfoAndColor("计划整改", Color.parseColor("#4B97F0"));
            }
//            holder.rectifi.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//
//                    ZhengaiDialog.getInstance(baseActivity, Integer.valueOf(topicinfoList.get(position).getZhengai()), new BaseListenner<Integer>() {
//                        @Override
//                        public void onResult(Integer result) {
//                            topicinfoList.get(position).setZhengai(result + "");
//                            if (topicinfoList.get(position).getZhengai().equals("0")) {
//                                holder.rectifi.setInfoAndColor("整改", Color.parseColor("#666666"));
//                            }
//                            if (topicinfoList.get(position).getZhengai().equals("1")) {
//                                holder.rectifi.setInfoAndColor("现场整改", Color.parseColor("#4B97F0"));
//                            }
//                            if (topicinfoList.get(position).getZhengai().equals("2")) {
//                                holder.rectifi.setInfoAndColor("计划整改", Color.parseColor("#4B97F0"));
//                            }
//                        }
//                    });
//
//
//                }
//            });

        } else if (topic_type.trim().equals("2")) {
            //==========================================================================================违法类型
            holder.spanerScore.setVisibility(View.INVISIBLE);
            holder.rectifi.setVisibility(View.INVISIBLE);
            holder.sp_type.setVisibility(View.VISIBLE);
            holder.sp_type_no.setVisibility(View.VISIBLE);

            holder.itemView.findViewById(R.id.layut_dafen).setVisibility(View.GONE);//隐藏打分
            holder.itemView.findViewById(R.id.layut_weifa).setVisibility(View.VISIBLE);//显示违法
//            holder.sp_type.setOnStateChangeListener(new BaseListenner<SingleChoose>() {
//                @Override
//                public void onResult(SingleChoose result) {
//                    if (result.isState()) {
//                        topicinfo.setZhengai("3");//违法
//                        holder.sp_type_no.setState(false);
//                    }
//                }
//            });
//            holder.sp_type_no.setOnStateChangeListener(new BaseListenner<SingleChoose>() {
//                @Override
//                public void onResult(SingleChoose result) {
//                    if (result.isState()) {
//                        topicinfo.setZhengai("0");//不违法
//                        holder.sp_type.setState(false);
//                    }
//                }
//            });
//            holder.sp_type.setTouchEvent(false);
//            holder.sp_type.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    AlertDialog.getInstance(baseActivity, true, true).setMession("确认违法?").setPositive("确认", new View.OnClickListener() {
//                        @Override
//                        public void onClick(View view) {
//                            holder.sp_type.setState(true);
//                            AlertDialog.myAlertDialog.dismiss();
//                        }
//                    });
//                    //Log.e("test", "点击事件");
//                }
//            });

            if (topicinfo.getZhengai().equals("0")) {
                //不违法
                holder.sp_type.setState(false);
                holder.sp_type_no.setState(true);
            } else if (topicinfo.getZhengai().equals("3")) {
                //违法
                holder.sp_type.setState(true);
                holder.sp_type_no.setState(false);
            }
            //违法，不违法。禁止点击
            holder.sp_type.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    return true;
                }
            });
            holder.sp_type_no.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    return true;
                }
            });
//            holder.sp_type.setEnabled(false);
//            holder.sp_type_no.setEnabled(false);
        }

//        Log.e("test","下标:\t"+position+"\t类型:\t"+topicinfo.getTopic_type()+"\t整改:\t"+topicinfo.getZhengai());

        //高度調整
//        int height = 200;
//        if (topic_type.trim().equals("2")) {
//            height = 160;
//        }
//        height += holder.itemView.getResources().getDimensionPixelOffset(R.dimen.y200);
//        holder.itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout).getLayoutParams().height = ProportionUtils.getInstance().adapterInt(height) + (holder.txt_question.LineCount - 1) * ProportionUtils.getInstance().adapterInt(40);
//        holder.itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout).getLayoutParams().width = (int) ProportionUtils.getInstance().realWidthPixels;
//        holder.itemView.findViewById(R.id.dialog_micrometertableos_constraintLayout).requestLayout();


        if (position % 2 == 0) {
            Animation animation_alpha = AnimationUtils.loadAnimation(baseActivity, R.anim.zebra_right_in_without_alpha);
            holder.itemView.startAnimation(animation_alpha);
        } else {
            Animation animation_alpha = AnimationUtils.loadAnimation(baseActivity, R.anim.zebra_left_in_without_alpha);
            holder.itemView.startAnimation(animation_alpha);
        }

        //Log.e("test", "题号:\t" + i + "\t行数:\t" + holder.txt_question.LineCount);
    }

    @Override
    public int getItemCount() {
        return topicinfoList.size();
    }
}
